import { FormEvent } from "miniprogram/@types/Event"
import {storeBindingsBehavior} from "mobx-miniprogram-bindings"
import {planStore} from "@store/plans"
import dayjs from "dayjs"
import {get, remove} from "@utils/http.js"
import { getPlanBehavior } from "@mixin/plan"
// components/person-plan/person-plan.ts
Component({
  /**
   * 混入状态管理库代码
   */
  behaviors:[storeBindingsBehavior,getPlanBehavior],
  /**
   * 组件的属性列表
   */
  properties: {

  },
  /**
   * 组件的初始数据
   */
  data: {
    headerDate:dayjs().format("YYYY/MM月"),
    schoolRegions:[{values:["1","2","3"]},{values:["1","2","3"]}],
    form:{
      selectSchool:"华农",
      selectRegion:"泰山",
    },
    showSelectRegion:false,
    weekDate:[],
    dayTime:[],
    weekStatusList:[],
    planList:[]
  },
  storeBindings:{
    store:planStore,
    fields:["planList","weekStatusList","datePlanList"],
    actions:[
      "setPlanList",
      "setWeekStatusList",
      "setId"
    ]
  },
  /**
   * 组件的方法列表
   */
  methods: {
    handleShowSelection:function(){
      this.setData({
        showSelectRegion:true
      })
    },
    closeSelection:function(){
      this.setData({
        showSelectRegion:false
      })
      this.getPlan()
    },
    changeSchoolRegion:function(e:any){
      const{value}=e.detail
      this.setData({
        form:{
          ...this.data.form,
          selectSchool:value[0],
          selectRegion:value[1]
        }
      })
    },
    getPlanDetail:function({currentTarget:{dataset:{item}}}:FormEvent<any>){
      this.setId(item.id)
      wx.navigateTo({
        url:`/pages/plan/plan-detail/plan-detail`,
      })
    },
    topPlan({currentTarget:{dataset:{item:{id,top}}},detail}:FormEvent<any>){
      const list=this.data.planList.map(item=>{
        if(item.id===id){
          item.top=detail
        }
        return item
      })
      this.setPlanList(list)
      get("/plan/top",{
        id,
        isTop:detail
      })
    },
    init:function(){
      const week=["日","一","二","三","四","五","六"]
      const weekDate=week.map((item,index)=>{
        return {
          date:item,
          day:dayjs().startOf(("week")).add(index,"day").get("date")
        }
      })
      const startTime=dayjs().set("h",8).set("m",0)
      const dayTime=[]
      for(let i=0;i<7;i++){
        dayTime.push(startTime.add(i*2,"h").format("HH:mm"))
      }
      this.setData({
        weekDate,
        dayTime
      })
    },
    createPlan:function(){
      wx.navigateTo({
        url:"/pages/plan/create-plan/create-plan"
      })
    },
    deletePlan:async function(e:FormEvent<any>){
      await remove("/plan",{
        id:e.detail
      })
      await this.getPlan()
    }
  },
  attached:function(){
    this.getPlan()
    this.init()
  }, 
})